<template>
  <page-header-wrapper>
    <a-card>    
      <JeepaySearchForm :searchConditionNum="6" :searchFunc="searchFunc" :resetFunc="onReset">
        <a-form-item label="" class="table-search-item">
          <JeepayDateRangePicker ref="dateRangePicker" v-model:value="vdata.searchData['queryDateRange']" customDateRangeType="dateTime" />
        </a-form-item>

        <JeepaySelect ref="orderKeyTypeRef" :reset="isReset">
          <a-select v-model:value="vdata.orderKeyType" @change="orderKeyTypeChangeFunc">
            <a-select-option v-for="item in orderKeyList" :key="item.key" :value="item.key">{{ item.desc }}</a-select-option>
          </a-select>
        </JeepaySelect>
        <jeepay-text-up v-model:value="vdata.searchData[vdata.orderKeyType]" :placeholder="orderKeyList.find(item => item.key==vdata.orderKeyType)?.desc" class="table-search-item" />

        <JeepaySearchInfoInput v-model:value="vdata.searchData.mchNo" placeholder="商户号" :textUpStyle="true" :mchNoAndName="true" showType="MCH" />
        <jeepay-text-up v-model:value="vdata.searchData.storeId" :placeholder="'门店ID'" class="table-search-item" />
        <jeepay-text-up v-model:value="vdata.searchData.storeName" :placeholder="'门店名称'" class="table-search-item" />
        <jeepay-text-up v-if="$hasAgentEnt()" v-model:value="vdata.searchData.agentNo" :placeholder="'代理商号'" class="table-search-item" />
        <jeepay-text-up v-model:value="vdata.searchData.isvNo" :placeholder="'服务商号'" class="table-search-item" />
        <JeepaySelect ref="stateRef" placeholder="订单状态" :reset="isReset">
          <a-select v-model:value="vdata.searchData.state" @change="stateRef.textupHandle()">
            <a-select-option value="">全部</a-select-option>
            <a-select-option value="0">订单生成</a-select-option>
            <a-select-option value="1">支付中</a-select-option>
            <a-select-option value="2">支付成功</a-select-option>
            <a-select-option value="3">支付失败</a-select-option>
            <a-select-option value="4">已撤销</a-select-option>
            <a-select-option value="5">已退款</a-select-option>
            <a-select-option value="6">订单关闭</a-select-option>
          </a-select>
        </JeepaySelect>
        
        <JeepaySelect ref="wayCodeRef" placeholder="支付方式" :reset="isReset">
          <a-select v-model:value="vdata.searchData.wayCode" @change="wayCodeRef.textupHandle()">
            <a-select-option value="">全部</a-select-option>
            <a-select-option v-for="item in payWayList" :key="item.wayCode" :value="item.wayCode">
              {{ item.wayName }}
            </a-select-option>
          </a-select>
        </JeepaySelect>

        <JeepaySelect ref="ifCodeRef" placeholder="支付接口" :reset="isReset">
          <a-select v-model:value="vdata.searchData.ifCode" @change="ifCodeRef.textupHandle()">
            <a-select-option value="">全部</a-select-option>
            <template v-for="(item) in vdata.ifCodeList" :key="item.ifCode">
              <a-select-option :value="item.ifCode">{{ item.ifName }}[{{ item.ifCode }}]</a-select-option>
            </template>
          </a-select>
        </JeepaySelect>
        
        <JeepaySearchInfoInput v-model:value="vdata.searchData.appId" placeholder="应用AppId" :textUpStyle="true" showType="MCH_APP" />

        <JeepaySelect ref="notifyStateRef" placeholder="回调状态" :reset="isReset"> 
          <a-select v-model:value="vdata.searchData.notifyState" @change="notifyStateRef.textupHandle()">
            <a-select-option value="">全部</a-select-option>
            <a-select-option value="0">未发送</a-select-option>
            <a-select-option value="1">已发送</a-select-option>
          </a-select>
        </JeepaySelect>
        
        <!-- @change="selectChange" -->
        <JeepaySelect ref="divisionStateRef" placeholder="分账状态" :reset="isReset">
          <a-select v-model:value="vdata.searchData.divisionState" @change="divisionStateRef.textupHandle()">
            <a-select-option value="">全部</a-select-option>
            <a-select-option value="0">未发生分账</a-select-option>
            <a-select-option value="1">等待分账任务处理</a-select-option>
            <a-select-option value="2">分账处理中</a-select-option>
            <a-select-option value="3">分账任务已结束（状态请看分账记录）</a-select-option>
          </a-select>
        </JeepaySelect>

        <jeepay-text-up v-if="$hasMemberEnt()" v-model:value="vdata.searchData.mbrTel" :placeholder="'会员手机号'" class="table-search-item" />
      </JeepaySearchForm>

      <!-- 列表渲染 -->
      <JeepayTable
        ref="infoTable"
        :initData="true"
        :reqTableDataFunc="reqTableDataFunc"
        :tableColumns="tableColumns"
        :searchData="vdata.searchData"
        rowKey="payOrderId"
        :tableRowCrossColor="true"
        :statisticsIsShow="$access('ENT_ORDER_COUNT')"
        :autoRefTableConfig="{defaultOpen: true, interval: 180}"
        :tableExportFunc="tableExportFunc"
        @btnLoadClose="btnLoading=false"
      >
        <template #statistics>
          <div class="statistics-list">
            <div v-for="(item, index) in orderCountList" :key="index" class="item">
              <div v-if="item.type == 'line'" class="line" />
              <div class="title">{{ item.title }}</div>
              <div v-if="item.title" class="amount" :style="{color: item.textColor}">
                <!-- <span v-if="item.symbol" class="symbol">{{ item.symbol == 'add' ? '+' : '-' }}</span> -->
                <span class="amount-num">{{ item.content }}</span>元
              </div>
              <div v-if="item.count >= 0" class="detail">
                <span>{{ item.count }}笔</span>
                <span v-if="item.detail" class="detail-text" @click="payDetail.showModal()">明细</span>
              </div>
            </div>
          </div>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'amount'">￥{{ record.amount/100 }}</template>
          <template v-if="column.key === 'refundAmount'">￥{{ record.refundAmount/100 }}</template>
          <template v-if="column.key == 'mchFeeAmount'">￥{{ (record.mchFeeAmount / 100).toFixed(2) }}</template>
          <template v-if="column.key == 'mchOrderFeeAmount'">￥{{ (record.mchOrderFeeAmount / 100).toFixed(2) }}</template>
          <template v-if="column.key === 'state'">
            <a-tag
              :key="record.state"
              :color="record.state === 0?'blue':record.state === 1?'orange':record.state === 2?'green':record.state === 6?'':'volcano'"
            >
              {{ record.state === 0?'订单生成':record.state === 1?'支付中':record.state === 2?'支付成功':record.state === 3?'支付失败':record.state === 4?'已撤销':record.state === 5?'已退款':record.state === 6?'订单关闭':'未知' }}
            </a-tag>
          </template>
          <template v-if="column.key === 'divisionState'">
            <span v-if="record.divisionState == 0"> - </span>
            <a-tag v-else-if="record.divisionState == 1" color="orange">待分账</a-tag>
            <a-tag v-else-if="record.divisionState == 2" color="red">分账处理中</a-tag>
            <a-tag v-else-if="record.divisionState == 3" color="green">任务已结束</a-tag>
            <span v-else>未知</span>
          </template>
          <template v-if="column.key === 'notifyState'"> <a-badge :status="record.notifyState === 1?'processing':'error'" :text="record.notifyState === 1?'已发送':'未发送'" /></template>
          <template v-if="column.key === 'orderNo'">
            <div class="order-list">
              <p><span style="color:#729ED5;border-radius: 1px;padding: 3px 7px;height: auto;width: auto;">支付</span>{{ record.payOrderId }}</p>
              <p style="margin-bottom: 0">
                <span style="color:#56cf56;border-radius: 1px;padding: 3px 7px;height: auto;width: auto;">商户</span>
                <a-tooltip v-if="record.mchOrderNo.length > record.payOrderId.length" placement="bottom" style="font-weight: normal;">
                  <template #title>
                    <span>{{ record.mchOrderNo }}</span>
                  </template>
                  {{ changeStr2ellipsis(record.mchOrderNo, record.payOrderId.length) }}
                </a-tooltip>
                <span v-else style="font-weight: normal;">{{ record.mchOrderNo }}</span>
              </p>
              <p v-if="record.channelOrderNo" style="margin-bottom: 0;margin-top: 10px">
                <span style="color:#fff;background:#E09C4D;">渠道</span>
                <a-tooltip v-if="record.channelOrderNo.length > record.payOrderId.length" placement="bottom" style="font-weight: normal;">
                  <template #title>
                    <span>{{ record.channelOrderNo }}</span>
                  </template>
                  {{ changeStr2ellipsis(record.channelOrderNo, record.payOrderId.length) }}
                </a-tooltip>
                <span v-else style="font-weight: normal;">{{ record.channelOrderNo }}</span>
              </p>
            </div>
          </template>
          <template v-if="column.key === 'ifCode'">
            <span v-if="record.ifCode">{{ record.ifName }} ({{ record.ifCode }})</span>
            <span v-else />
          </template>

          <template v-if="column.key === 'op'">
            <!-- 操作列插槽 -->
            <JeepayTableColumns>
              <a-button v-if="$access('ENT_PAY_ORDER_VIEW')" type="link" @click="detailFunc(record.payOrderId)">详情</a-button>
              <a-button v-if="$access('ENT_PAY_ORDER_REFUND')" v-show="(record.state === 2 && record.refundState !== 2)" type="link" style="color: red" @click="openFunc(record, record.payOrderId)">退款</a-button>
            </JeepayTableColumns>
          </template>
        </template>
      </JeepayTable>
    </a-card>
    <!-- 退款弹出框 -->
    <refund-modal ref="refundModalInfo" :callbackFunc="searchFunc" />
    <!-- 日志详情抽屉 -->
    <template>
      <a-drawer
        width="50%"
        placement="right"
        :closable="true"
        :visible="visible"
        :title="visible === true? '订单详情':''"
        @close="onClose"
      >
        <a-row justify="space-between" type="flex">
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="商户类型">
                {{ detailData.mchType === 1?'普通商户':detailData.mchType === 2?'特约商户':'未知' }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="服务商号">
                {{ detailData.isvNo }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="支付订单号">
                <a-tag color="purple">
                  {{ detailData.payOrderId }}
                </a-tag>
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col v-if="$hasAgentEnt() && detailData.agentNo" :sm="12">
            <a-descriptions>
              <a-descriptions-item label="代理商号">
                {{ detailData.agentNo }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="商户号">
                {{ detailData.mchNo }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="商户订单号">
                {{ detailData.mchOrderNo }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="商户名称">
                {{ detailData.mchName }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="门店名称">
                {{ detailData.storeName }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="门店ID">
                {{ detailData.storeId }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="支付金额">
                <a-tag color="green">
                  {{ detailData.amount/100 }}
                </a-tag>
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions><a-descriptions-item label="实际手续费"><a-tag color="pink">{{ detailData.mchFeeAmount/100 }}</a-tag></a-descriptions-item></a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions><a-descriptions-item label="收单手续费"><a-tag color="pink">{{ detailData.mchOrderFeeAmount/100 }}</a-tag></a-descriptions-item></a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions><a-descriptions-item label="商家费率">{{ detailData.mchFeeRate }}</a-descriptions-item></a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="订单状态">
                <a-tag :color="detailData.state === 0?'blue':detailData.state === 1?'orange':detailData.state === 2?'green':detailData.state === 6?'':'volcano'">
                  {{ detailData.state === 0?'订单生成':detailData.state === 1?'支付中':detailData.state === 2?'支付成功':detailData.state === 3?'支付失败':detailData.state === 4?'已撤销':detailData.state === 5?'已退款':detailData.state === 6?'订单关闭':'未知' }}
                </a-tag>
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="回调状态">
                <a-tag :color="detailData.notifyState === 1?'green':'volcano'">
                  {{ detailData.notifyState === 0?'未发送':detailData.notifyState === 1?'已发送':'未知' }}
                </a-tag>
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="应用APPID">
                {{ detailData.appId }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="支付错误码">
                {{ detailData.errCode }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="支付错误描述">
                {{ detailData.errMsg }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="买家备注">
                {{ detailData.buyerRemark }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="卖家备注">
                {{ detailData.sellerRemark }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col v-if="$hasMemberEnt() && detailData.mbrId" :sm="12">
            <a-descriptions>
              <a-descriptions-item label="会员ID">
                {{ detailData.mbrId }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col v-if="$hasMemberEnt() && detailData.mbrTel" :sm="12">
            <a-descriptions>
              <a-descriptions-item label="会员手机号">
                {{ detailData.mbrTel }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="订单失效时间">
                {{ detailData.expiredTime }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="支付成功时间">
                {{ detailData.successTime }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="创建时间">
                {{ detailData.createdAt }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="更新时间">
                {{ detailData.updatedAt }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-divider />
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="商品标题">
                {{ detailData.subject }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="商品描述">
                {{ detailData.body }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="接口代码">
                {{ detailData.ifCode }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="货币代码">
                {{ detailData.currency }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="支付方式">
                {{ detailData.wayCode }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="客户端IP">
                {{ detailData.clientIp }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="渠道服务商机构号">
                {{ detailData.channelIsvNo }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="渠道商户号">
                {{ detailData.channelMchNo }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="用户标识">
                {{ detailData.channelUser }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="渠道订单号">
                {{ detailData.channelOrderNo }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="用户支付凭证交易单号">
                {{ detailData.platformOrderNo }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="用户支付凭证商户单号">
                {{ detailData.platformMchOrderNo }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="异步通知地址">
                {{ detailData.notifyUrl }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="页面跳转地址">
                {{ detailData.returnUrl }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="退款状态">
                <a-tag :color="detailData.refundState === 0?'blue':detailData.refundState === 1?'orange':detailData.refundState === 2?'green':'volcano'">
                  {{ detailData.refundState === 0?'未发起':detailData.refundState === 1?'部分退款':detailData.refundState === 2?'全额退款':'未知' }}
                </a-tag>
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="退款次数">
                {{ detailData.refundTimes }}
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="退款总额">
                <a-tag v-if="detailData.refundAmount" color="cyan">
                  {{ detailData.refundAmount/100 }}
                </a-tag>
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-divider />
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="订单分账模式">
                <span v-if="detailData.divisionMode == 0">该笔订单不允许分账</span>
                <span v-else-if="detailData.divisionMode == 1">支付成功按配置自动完成分账</span>
                <span v-else-if="detailData.divisionMode == 2">商户手动分账(解冻商户金额)</span>
                <span v-else>未知</span>
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions>
              <a-descriptions-item label="分账状态">
                <a-tag v-if="detailData.divisionState == 0" color="blue">未发生分账</a-tag>
                <a-tag v-else-if="detailData.divisionState == 1" color="orange">待分账</a-tag>
                <a-tag v-else-if="detailData.divisionState == 2" color="red">分账处理中</a-tag>
                <a-tag v-else-if="detailData.divisionState == 3" color="green">任务已结束</a-tag>
                <a-tag v-else color="#f50">未知</a-tag>
              </a-descriptions-item>
            </a-descriptions>
          </a-col>
          <a-col :sm="12">
            <a-descriptions><a-descriptions-item label="最新分账发起时间">{{ detailData.divisionLastTime }}</a-descriptions-item></a-descriptions>
          </a-col>
        </a-row>
        <a-divider />
        <a-row justify="start" type="flex">
          <a-col :sm="24">
            <a-form-item label="扩展参数">
              <a-textarea
                v-model:value="detailData.extParam"
                disabled="disabled"
                style="height: 100px;color: black"
              />
            </a-form-item>
          </a-col>
        </a-row>

        <a-row justify="start" type="flex">
          <a-col :sm="24">
            <a-form-item label="分润情况">
              <div v-if="detailData && detailData.profitList" style="padding-top: 5px; margin-left:10px">
                <p v-for="(item) in detailData.profitList.filter( r => r.infoType == 'MCH')" :key="item.infoId">
                  商户（{{ item.infoName }}） 手续费 : {{ (item.calFeeAmount / 100).toFixed(2) }} 元
                </p>
                <div v-if="$hasAgentEnt()">
                  <p v-for="(item) in detailData.profitList.filter( r => r.infoType == 'AGENT')" :key="item.infoId">
                    代理商（ {{ item.infoName }}）分润 : {{ (item.calProfitAmount / 100).toFixed(2) }} 元
                  </p>
                </div> 
                
                <p v-for="(item) in detailData.profitList.filter( r => r.infoId == 'PLATFORM_INACCOUNT')" :key="item.infoId">
                  平台三方入账 <span v-if="$hasAgentEnt()">（不扣减代理商分润）</span>: {{ (item.calProfitAmount / 100).toFixed(2) }} 元
                </p>
                <p v-for="(item) in detailData.profitList.filter( r => r.infoId == 'PLATFORM_PROFIT')" :key="item.infoId">
                  平台利润: {{ (item.calProfitAmount / 100).toFixed(2) }} 元
                </p>
              </div>
            </a-form-item>
          </a-col>
        </a-row>
      </a-drawer>
    </template>

    <PayDetail ref="payDetail" :countDetailList="countDetailList" />
  </page-header-wrapper>
</template>
<script setup lang='ts' >
import PayDetail from './PayDetail.vue' // 数据统计详情弹出框
import RefundModal from './RefundModal.vue' // 退款弹出框
import { API_URL_PAY_ORDER_LIST, API_URL_PAYWAYS_LIST, req, $exportExcel, exportExcelUrl, $payOrderCount, API_URL_IFDEFINES_LIST } from '@/api/manage'
import { ref, reactive, getCurrentInstance, onMounted, nextTick, toRaw } from 'vue'
import fileDownload from 'js-file-download'
import { resourceUsage } from 'process'

// 导入全局函数
const { $infoBox, $access, $hasAgentEnt, $hasMemberEnt } = getCurrentInstance()!.appContext.config.globalProperties

// infoTable组件
const infoTable = ref()
const refundModalInfo = ref()
const payDetail = ref() // 数据统计详情弹窗
const ifCodeRef = ref()
const dateRangePicker = ref()

const vdata : any = reactive({
  searchMchShowType : 'MCH',
  searchData: {queryDateRange: 'today'},
  orderKeyType: 'payOrderId'
})

const orderKeyList = ref([
  { 'key': 'payOrderId', 'desc': '支付订单号' },
  { 'key': 'mchOrderNo', 'desc': '商户订单号' },
  { 'key': 'channelOrderNo', 'desc': '渠道订单号' },
  { 'key': 'platformOrderNo', 'desc': '用户支付凭证交易单号' },
  { 'key': 'platformMchOrderNo', 'desc': '用户支付凭证商户单号' }])

// eslint-disable-next-line no-unused-vars
const tableColumns = ref([
  { key: 'orderNo', title: '订单号', fixed: 'left' },
  { key: 'amount', title: '支付金额', ellipsis: true },
  { key: 'refundAmount', title: '退款金额' },
  { key: 'mchFeeAmount', title: '实际手续费', dataIndex: 'mchFeeAmount' },
  { key: 'mchOrderFeeAmount', title: '收单手续费', dataIndex: 'mchOrderFeeAmount' },
  { key: 'mchName', title: '商户名称', dataIndex: 'mchName', ellipsis: true },
  { key: 'storeName', title: '门店名称', dataIndex: 'storeName', ellipsis: true, defaultHidden: true },
  { key: 'storeId', title: '门店ID', dataIndex: 'storeId', defaultHidden: true },
  { key: 'ifCode', title: '支付接口'},
  { key: 'wayName', title: '支付方式', dataIndex: 'wayName' },
  { key: 'state', title: '支付状态' },
  { key: 'notifyState', title: '回调状态' },
  { key: 'divisionState', title: '分账状态' },
  { key: 'buyerRemark', title: '买家备注', dataIndex: 'buyerRemark', defaultHidden: true },
  { key: 'platformMchOrderNo', title: '支付凭证商户单号', dataIndex: 'platformMchOrderNo', defaultHidden: true },
  { key: 'platformOrderNo', title: '支付凭证交易单号', dataIndex: 'platformOrderNo', defaultHidden: true },
  { key: 'createdAt', dataIndex: 'createdAt', title: '创建日期' },
  { key: 'op', title: '操作', fixed: 'right', align: 'center' }
])

let btnLoading = ref(false)

let visible = ref(false)
let detailData:any = ref({})
let payWayList:any = ref([])

  onMounted( () => {
    if ($access('ENT_PAY_ORDER_SEARCH_PAY_WAY')) {
      initPayWay()
    }
    if($access('ENT_PC_IF_DEFINE_LIST')){
      req.list(API_URL_IFDEFINES_LIST, {state:1}).then((res) => {
        vdata.ifCodeList = res
      })
    }
  })

  let orderCountList:any = ref([]) // 数据统计数组
  let countDetailList: any = ref([]) // 数据统计明细数组
  // 数据统计
  const getOrderCount = () => {

    if(!$access('ENT_ORDER_COUNT')){
      return false
    }

    $payOrderCount(vdata.searchData).then( res => {
      orderCountList.value = [ 
        {title: '实际收款金额', symbol: 'add', textColor: '#1A66FF', content: ((res.allAmount - res.mchFeeAmount) / 100).toFixed(2)},
        {type: 'line'},
        {title: '成交订单', content: (res.allAmount / 100).toFixed(2), count: res.allCount, detail: true},
        {type: 'line'},
        {title: '手续费金额',symbol: 'sub', content: (res.mchFeeAmount / 100).toFixed(2)},
        {type: 'line'},
        {title: '退款订单', content: (res.refundAmount / 100).toFixed(2), count: res.refundCount, textColor: '#faad14'},
      ]
      countDetailList.value = [
        {title: '创建订单', content: (res.allPayAmount / 100).toFixed(2), count: res.allPayCount},
        {type: 'line'},
        {title: '成交订单', content: (res.allAmount / 100).toFixed(2), count: res.payCount},
        {type: 'line'},
        {title: '未付款订单', content: (res.failPayAmount / 100).toFixed(2), count: res.failPayCount},
      ]
    })
  }
  getOrderCount() // 进入页面时调用一次

    // 请求table接口数据
    function reqTableDataFunc(params) {
      return req.list(API_URL_PAY_ORDER_LIST, params)
    }
    
    function searchFunc () { // 点击【查询】按钮点击事件
      btnLoading.value = true
      infoTable.value.refTable(true)
      getOrderCount() // 点击查询按钮，调用数据统计方法
    }
    
    // 打开退款弹出框
    function openFunc (record, recordId) {
      if (record.refundState === 2) {
        return $infoBox.modalError('订单无可退款金额', '')
      }
      refundModalInfo.value.show(recordId)
    }
    
    function detailFunc (recordId) {
      detailData.value = {}
      req.getById(API_URL_PAY_ORDER_LIST, recordId).then(res => {
         Object.assign(detailData.value, res)
      })
      visible.value = true
    }
    
    function onClose () {
      visible.value = false
    }
    
    function initPayWay () {
      req.list(API_URL_PAYWAYS_LIST, { 'pageSize': -1 }).then(res => { // 支付方式下拉列表
        payWayList.value = Object.assign([], res.records)
      })
    }
    
    function changeStr2ellipsis (orderNo, baseLength) {
      const halfLengh = baseLength / 2
      return orderNo.substring(0, halfLengh - 1) + '...' + orderNo.substring(orderNo.length - halfLengh, orderNo.length)
    }
    
    function tableExportFunc(){
      return $exportExcel(exportExcelUrl.payOrder, Object.assign({}, vdata.searchData, {'pageSize': -1})).then(res => {
         fileDownload(res.data, '订单列表.xlsx')
      }).catch ((error) =>{console.log(error)} )
    }

    // 获取下拉框组件
    const wayCodeRef = ref()
    const stateRef = ref()
    const orderKeyTypeRef = ref()
    const notifyStateRef = ref()
    const divisionStateRef = ref()
    let isReset = ref(0) // 下拉搜索框是否重置

    function onReset(){
      isReset.value++ // 下拉搜索框重置
      //重置搜索内容
      dateRangePicker.value.returnSelectModel()
      vdata.searchData = { queryDateRange: 'today' }
    }

    function orderKeyTypeChangeFunc() {
      orderKeyTypeRef.value.textupHandle()

      vdata.searchData.payOrderId = ''
      vdata.searchData.mchOrderNo = ''
      vdata.searchData.channelOrderNo = ''
      vdata.searchData.platformMchOrderNo = ''
      vdata.searchData.platformOrderNo = ''
    }


</script>
<style lang="less" scoped>

.order-list {
  -webkit-text-size-adjust:none;
  font-size: 12px;
  display: flex;
  flex-direction: column;

  p {
    white-space:nowrap;
    span {
      display: inline-block;
      font-weight: 800;
      height: 16px;
      line-height: 16px;
      width: 35px;
      border-radius: 5px;
      text-align: center;
      margin-right: 2px;
    }
  }
}
</style>
